package View;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import presenter.Driver;
import presenter.OfficePresenter;

public class MainWindow
{
	public static final int XCENTER = Toolkit.getDefaultToolkit().getScreenSize().width/2;
	public static final int YCENTER = Toolkit.getDefaultToolkit().getScreenSize().height/2;
	  
	private JFrame frame;
	private int myRole;

	/**
	 * Create the application.
	 */
	public MainWindow(int role)
	{
		initialize();
	}
	
	/**
	 * Initializes the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Doctor's Office Application");
		frame.setBounds(XCENTER - 350, YCENTER - 250, 700, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmNew = new JMenuItem("New");
		mnFile.add(mntmNew);
		
		JMenuItem mntmOpen = new JMenuItem("Open Office from File");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int res = chooser.showDialog(null, "Load Saved Employees");
				if (res == JFileChooser.APPROVE_OPTION) {
				    Driver.myOfficePresenter.load(chooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSave = new JMenuItem("Save Office");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int res = chooser.showDialog(null, "Save Employee Data");
				if (res == JFileChooser.APPROVE_OPTION) {
				    Driver.myOfficePresenter.save(chooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		mnFile.add(mntmSave);
		
		JMenuItem mntmLogout = new JMenuItem("Logout");
		mnFile.add(mntmLogout);
		mntmLogout.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				Driver.myOfficePresenter.logOut();
			}
		});
		JMenuItem mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
		
		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);
		
		JMenu mnView = new JMenu("View");
		menuBar.add(mnView);
		
		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);
		
		switch(myRole)
		{
			case 0: //patient
			{
				PatientPanel pPanel = new PatientPanel();
				frame.add(pPanel);
				break;
			}
		    
			case 1: //nurse
			{
				NursePanel nPanel = new NursePanel();
				frame.add(nPanel);
				break;
			}
		    
			case 2: // administrator
			{	
				AdminPanel aPanel = new AdminPanel();
				frame.add(aPanel);
				break;
			}
			
			case 3: // doctor
			{	
				DoctorPanel dPanel = new DoctorPanel();
				frame.add(dPanel);
				break;
			}
		}  
		
		frame.setVisible(true);
	}
	
	public void close()
	{
		frame.dispose();
	}
}
